package com.quick.android.utils;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间格式转换工具
 **/
@SuppressLint("SimpleDateFormat")
public class TimeUtils {
	/** 获取当天日期，格式为yyyy-MM-dd */
	public static String getCurrentDay() {
		Long time = new Long(new Date().getTime());
		return dataFormat(time, "yyyy-MM-dd");
	}
	/** 获取当前时间，格式为yyyy/MM/dd HH:mm */
	public static String getCurrentTime() {
		Long time = new Long(new Date().getTime());
		return dataFormat(time, "yyyy/MM/dd HH:mm");
	}
	/** 时间格式转换，将long 转  yyyy-MM-dd*/
	public static String getTimeFromLong(long time) {
		return dataFormat(time, "yyyy-MM-dd");
	}
	/** 时间格式转换，將格式为yyyyMMddHHmmss转为yyyy/MM/dd */
	public static String getCorrectDay(String strTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Long time = (long) 0;
		try {
			time = format.parse(strTime).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dataFormat(time, "yyyy/MM/dd");
	}
	/** 时间格式转换，将 yyyy/MM/dd HH:mm 转 long */
	public static long getLongTime(String strTime) {
		return parseTimeToLong(strTime, "yyyy/MM/dd HH:mm");
	}
	/** 时间格式转换，将HH:mm 转 long */
	public static long getLongTimeHHmm(String strTime) {
		return parseTimeToLong(strTime, "HH:mm");
	}
	/** 计算日期差，按天数算 */
	public static int countDays(long oldTime) {
		long time = new Date().getTime();
		long objectTime = time - oldTime;
		int days = (int) (objectTime / (3600 * 1000 * 24));
		return days;
	}
	/** 返回格式化的日期，yyyy-MM-dd */
	public static String getStrTimeFromYMD(int year, int month, int day) {
		Calendar c = Calendar.getInstance();
		c.set(year, month, day);
		return dataFormat(c.getTime(), "yyyy-MM-dd");
	}
	
	public static String dataFormat(Object obj, String format){
		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
		String d = mSimpleDateFormat.format(obj);
		return d;
	}
		
	public static long parseTimeToLong(String time, String format){
		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
		long parseTime = 0;
		try {
			parseTime = mSimpleDateFormat.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parseTime;
	}
	
}
